package com.jsh.erp.utils;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.ExceptionConstants;
import com.jsh.erp.exception.BusinessRunTimeException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

public class NoticeUtil {



    public static String sendPost(String prefixUrl,JSONObject object) {
//        String url = "http://47.106.111.207:9999";
        String url = prefixUrl;
//        String url = "http://localhost:9999/jshERP-boot/systemConfig/synchronization";
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "application/json, text/plain, */*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

//            conn.setRequestProperty("X-Access-Token",token);
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(3000000);
            conn.setReadTimeout(3000000);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(object);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
//            System.out.println("---------------------" + in.readLine());
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE, "发送 POST 请求出现异常");
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }
    public static String sendGetMethod(String url, String params,
                                       String encoding) throws Exception {
        String result = "";
        url += ((-1 == url.indexOf("?")) ? "?" : "&") + params;

        CloseableHttpClient client = HttpClientBuilder.create().build();
        // post方式:HttpPost post =new HttpPost(url);
        //put方式:HttpPut post =new HttpPut(url);
        HttpGet get = new HttpGet(url);
        get.setHeader("charset", encoding);
        //System.out.print("url:"+url);
        try {
            HttpResponse response = client.execute(get);
            System.out.println("statusCode:"+response.getStatusLine().getStatusCode());
            if (200 ==response.getStatusLine().getStatusCode()) {
                result = EntityUtils.toString(response.getEntity(), encoding);
            } else {
                throw new Exception("Invalide response from Api!"
                        + EntityUtils.toString(response.getEntity(), encoding));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.print("result:"+result);
        return result;
    }


}
